package com.sardine.base.esm.session;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.List;

import static com.sardine.base.esm.session.EsmSessionUtils.*;
import static com.sardine.base.esm.session.EsmSessionUtils.getEsmSession;
import static java.lang.reflect.Proxy.newProxyInstance;

/**
 * 上海京东到家元信信息技术有限公司<br>
 * Created with IntelliJ IDEA.
 * Date:2017/5/22
 * Time:9:59
 *
 * @author daifeng<br>
 *         daifeng@imdada.cn
 * @since JDK 1.7 <br>
 * Copyright (c) 2017 imdada System Incorporated All Rights Reserved.
 */
public class EsmSessionTemplate implements EsmSession {
    private static final Logger logger = LoggerFactory.getLogger(EsmSessionTemplate.class);

    private final EsmSessionFactory sessionFactory;
    private final EsmExecutorType esmExecutorType;
    private final EsmSession esmSessionProxy;

    public EsmSessionTemplate(EsmSessionFactory sessionFactory) {
        this(sessionFactory,sessionFactory.getConfiguration().getDefaultEsmExecutorType());
    }

    public EsmSessionTemplate(EsmSessionFactory sessionFactory,EsmExecutorType esmExecutorType) {
        this.sessionFactory = sessionFactory;
        this.esmExecutorType = esmExecutorType;
        //生产一个代理session
        this.esmSessionProxy =(EsmSession) newProxyInstance(EsmSessionFactory.class.getClassLoader(),new Class[]{EsmSession.class},new EsmSessionInterceptor());
        logger.info("生产一个代理EsmSession完成...");
    }


    public EsmSessionFactory getSessionFactory() {
        return sessionFactory;
    }
    @Override
    public <T> T selectOne(String statement) {
        return null;
    }

    @Override
    public <E> List<E> selectList(String statement) {
        return null;
    }

    @Override
    public int insert(String statement) {
        return 0;
    }

    @Override
    public int update(String statement) {
        return 0;
    }

    @Override
    public int delete(String statement) {
        return 0;
    }

    @Override
    public void close() throws IOException {
        throw new RuntimeException("不能手动关闭一个spring所管理的 EsmSession");
    }

    /**
     * 获取类的映射
     *
     * @param type
     * @return
     */
    @Override
    public <T> T getMapper(Class<T> type) {
        return getConfiguration().getMapper(type, this);
    }

    /**
     * 得到ESM 相关配置信息
     *
     * @return
     */
    @Override
    public Configuration getConfiguration() {
        return this.sessionFactory.getConfiguration();
    }




    private class EsmSessionInterceptor implements InvocationHandler {
        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            final EsmSession esmSession= getEsmSession(sessionFactory, esmExecutorType);
            try {
                Object result = method.invoke(esmSession, args);
                return result;
            } catch (Throwable e) {
               throw new Throwable(e);
            }finally {
                logger.info("执行别的操作...");
            }
        }
    }
}
